package com.example.second.Dao;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.Map;

@Repository
public class StockDaoImpl implements IStockDao{

    @Autowired
    JdbcTemplate jdbcTemplate;

    public ArrayList<Map<String,Object>> getStockList(){
        String sql = " SELECT id AS sku_id ,title,images,stock,price,indexs,own_spec FROM tb_sku ";
        ArrayList<Map<String,Object>> list = (ArrayList<Map<String,Object>>)jdbcTemplate.queryForList(sql);
        return list;
    }

    public ArrayList<Map<String,Object>> getStock(String sku_id){
        String sql = "SELECT tb_sku.title,tb_sku.images,tb_sku.stock,tb_sku.price,tb_sku.indexs,tb_sku.own_spec," +
                "tb_sku.enable,tb_sku.create_time,tb_sku.update_time,tb_sku.id AS sku_id,tb_spu_special.after_service,tb_spu_special.description," +
                "tb_spu_special.generic_spec,tb_spu_special.special_spec FROM tb_sku INNER JOIN  " +
                "ON tb_sku.spu_id = tb_spu_special.spu_id WHERE tb_sku.id = ? ";
//        采用参数化的查询可以在业务层对参数的合规性进行判断，防止sql注入
        ArrayList<Map<String,Object>> list = (ArrayList<Map<String,Object>>) jdbcTemplate.queryForList(sql,sku_id);
        return list;
    }

    public Boolean insertLimitPolicy(Map<String,Object> policyInfo){
        String sql = "INSERT INTO tb_limit_policy(sku_id,price,quanty,begin_time,end_time) VALUES(?,?,?,?,?)";

        Boolean result = jdbcTemplate.update(sql, policyInfo.get("sku_id"),policyInfo.get("price"),
                policyInfo.get("quanty"),policyInfo.get("begin_time"),policyInfo.get("end_time"))==1;
        return result;


    }
}
